// pages/user/user.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    tel: '暂无信息',
    email: '暂无信息',
    address: '暂无信息',
    job: '暂无信息',
    company: '暂无信息',
    userName: null,
    userImage: null,
  },

  /**
   * 获取用户信息
   */
  getUserInfo: function (e) {
    console.log(e)
    var that = this
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res);
        wx.request({
          url: app.globalData.url + 'Login/wechatLogin',
          method: 'POST',
          data: {
            code: res.code,
            nickname: that.data.userInfo.nickName,
            image: that.data.userInfo.avatarUrl
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (res) {
            console.log(res);
            wx.setStorage({
              key: 'openid',
              data: res.data.data.openid
            })
            wx.setStorage({
              key: 'session_key',
              data: res.data.data.session_key
            })
            console.log(wx.getStorageSync('openid'));
            console.log(wx.getStorageSync('session_key'));
            wx.request({
              url: app.globalData.url + 'Card/index',
              method: 'GET',
              data: {
                openid: wx.getStorageSync('openid'),
              },
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              success: function (res) {
                console.log(res);
                that.setData({
                  userImage: res.data.data.image,
                  userName: res.data.data.username,
                  job: res.data.data.job,
                  company: res.data.data.company,
                  tel: res.data.data.telephone,
                  email: res.data.data.email,
                  address: res.data.data.city + res.data.data.address
                })
              }

            })


          }

        })
      }
    })

  },
  toBuy() {
    wx.navigateTo({
      url: '../buy/buy',
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if (app.globalData.userInfo) {
      that.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (that.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        that.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    wx.request({
      url: app.globalData.url + 'Card/index',
      method: 'GET',
      data: {
        openid: wx.getStorageSync('openid'),
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res);
        that.setData({
          userImage: res.data.data.image,
          userName: res.data.data.username,
          job: res.data.data.job,
          company: res.data.data.company,
          tel: res.data.data.telephone,
          email: res.data.data.email,
          address: res.data.data.city+res.data.data.address
        })
      }

    })
  },
  /**
   * 跳转到编辑名片
   */
  editCard: function () {
    wx.navigateTo({
      url: '../editorcard/editorcard',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})